﻿// This code is released under the BSD license.
namespace PugLib.UI.Controls
{
    using System;
    using System.Drawing;

    public partial class PugInstructionalTextBox : PugTextBox
    {
        private readonly Color _defaultFontColor;
        private string _instructionalText = string.Empty;

        public PugInstructionalTextBox()
        {
            InitializeComponent();

            _defaultFontColor = base.StateCommon.Content.Color1;

            SetInstructionalFont();

            this.Enter += _Enter;
            this.Leave += _Leave;
        }

        /// <summary>
        /// Sets the instructional text and main Text property
        /// </summary>
        public string InstructionalText
        {
            get { return _instructionalText; }
            set
            {
                _instructionalText = value;
                this.Text = value;
            }
        }

        private void _Enter(object sender, EventArgs e)
        {
            // clear instructional text so they can just start typing in their meaningful text
            if (string.Equals(this.Text, _instructionalText, StringComparison.OrdinalIgnoreCase))
            {
                SetRegularFont();
                this.Text = string.Empty;
            }
        }

        private void SetRegularFont()
        {
            base.StateCommon.Content.Font = new Font(base.Font, FontStyle.Regular);
            base.StateCommon.Content.Color1 = _defaultFontColor;
        }

        private void _Leave(object sender, EventArgs e)
        {
            // if they didn't do anything, restore the instructional text
            if (string.IsNullOrEmpty(this.Text))
            {
                SetInstructionalFont();
                this.Text = _instructionalText;
            }
        }

        private void SetInstructionalFont()
        {
            base.StateCommon.Content.Font = new Font(base.Font, FontStyle.Italic);
            base.StateCommon.Content.Color1 = Color.FromArgb(64, 64, 64);
        }
    }
}